﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class SaleAmountChartController : BaseController<usersorder>
    {
        // 各店铺 销售额 统计 柱状图
        // GET: /SaleAmountChart/
        protected admin CurrentUer
        {
            get { return Session["CurrentUer"] == null ? null : (admin)Session["CurrentUer"]; }
        }
        public override BLL.BaseBll<usersorder> bll
        {
            get
            {
                return new UsersOrderBll();
            }

        }

        public class OrderModel
        {
            public string shopName { get; set; }
            public int storeid { get; set; }
            public decimal? SaleMoney { get; set; }
           // public string wechatName { get; set; }
        }
        public class PerPriceModel
        {
            public string shopName;
            public decimal SaleMoney;
        }

        List<OrderModel> OrderList = new List<OrderModel>();

        UserBll userbll = new UserBll();

        WaresBll waresBll = new WaresBll();

        StoreBll storeBll = new StoreBll();

        OrderDetailsBll orderDetailBll = new OrderDetailsBll();

        /// <summary>
        /// 销售额 统计图 饼状图 （之前的）
        /// </summary>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <returns></returns>

        public ActionResult GetOrderAll(string date1, string date2 )
        {         
            List<OrderModel> dataList = new List<OrderModel>();
            Expression<Func<usersorder, bool>> condition = null;    
            //  默认查询今天之前七天的记录 所有店铺的
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) )
            {
                DateTime todayNow = DateTime.Now.Date;
                DateTime midnight = DateTime.Now;
                midnight = midnight.Date.AddDays(+1);
                DateTime beforeWeek = DateTime.Now.Date.AddDays(-6);
                condition = x => x.orderStatus == 1 && x.orderTime.Date <= midnight && x.orderTime.Date >= beforeWeek;
                var query = bll.Search().ToList();
               
                var listOrder = query.Where(x => x.orderStatus == 1 && x.orderTime.Date <= todayNow && x.orderTime.Date >= beforeWeek).GroupBy(x => x.storeid).Select(x => new {orderSumprice = x.Sum(y => y.orderSumprice), x.FirstOrDefault().storeid, }).ToList();
                foreach (var oneShop in listOrder)
                {
                    OrderModel orderModel = new OrderModel();
                    orderModel.SaleMoney = oneShop.orderSumprice;
                    orderModel.shopName = storeBll.Search(x => x.storeid == oneShop.storeid).Select(x => new { x.storeid, x.storeName }).ToList().FirstOrDefault().storeName;
                    dataList.Add(orderModel); 
                }
            }
            ////根据店铺查询  七天的
            //if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2)&& storeid!=null)
            //{
            //    int storeID = Convert.ToInt32(storeid);
            //    DateTime todayNow = DateTime.Now.Date;
            //    DateTime midnight = DateTime.Now;
            //    midnight = midnight.Date.AddDays(+1);
            //    DateTime beforeWeek = DateTime.Now.Date.AddDays(-6);
            //    condition = x => x.orderStatus == 1 && x.orderTime.Date <= midnight && x.orderTime.Date >= beforeWeek;
            //    var query = bll.Search().ToList();

            //    var listOrder = query.Where(x => x.orderStatus == 1 && x.storeid==storeID && x.orderTime.Date <= todayNow && x.orderTime.Date >= beforeWeek).GroupBy(x => x.storeid).Select(x => new { orderSumprice = x.Sum(y => y.orderSumprice), x.FirstOrDefault().storeid, }).ToList();
            //    foreach (var oneShop in listOrder)
            //    {
            //        OrderModel orderModel = new OrderModel();
            //        orderModel.SaleMoney = oneShop.orderSumprice;
            //        orderModel.shopName = storeBll.Search(x => x.storeid == oneShop.storeid).Select(x => new { x.storeid, x.storeName }).ToList().FirstOrDefault().storeName;
            //        dataList.Add(orderModel);
            //    }
            //}
            //按日期查询   两个店铺对比
            if (date1 != null && date1 != "" && date2 != null && date2 != "")
            {
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                condition = x => x.orderStatus == 1 && x.orderTime >= datetime1 && x.orderTime <= datetime2;
                var listOrder = bll.Search().Where(condition).GroupBy(x => x.storeid).Select(x => new { x.FirstOrDefault().orderCode, orderSumprice = x.Sum(y => y.orderSumprice), x.FirstOrDefault().storeid, x.FirstOrDefault().userCode, x.FirstOrDefault().orderTime,}).ToList();
                foreach (var oneShop in listOrder)
                {
                    OrderModel orderModel = new OrderModel();
                    orderModel.SaleMoney = oneShop.orderSumprice;
                    orderModel.shopName = storeBll.Search(x => x.storeid == oneShop.storeid).Select(x => new { x.storeid, x.storeName }).ToList().FirstOrDefault().storeName;
                    dataList.Add(orderModel);//  
                }
            }
            ////按日期查询  和店铺查询 
            //if (date1 != null && date1 != "" && date2 != null && date2 != "" && storeid != null && storeid != "")
            //{
            //    int storeID = Convert.ToInt32(storeid);
            //    DateTime datetime1 = DateTime.Parse(date1);
            //    DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
            //    condition = x => x.orderStatus == 1 && x.orderTime >= datetime1 && x.orderTime <= datetime2&&x.storeid==storeID;
            //    var listOrder = bll.Search().Where(condition).GroupBy(x => x.storeid).Select(x => new { x.FirstOrDefault().orderCode, orderSumprice = x.Sum(y => y.orderSumprice), x.FirstOrDefault().storeid, x.FirstOrDefault().userCode, x.FirstOrDefault().orderTime, }).ToList();
            //    foreach (var oneShop in listOrder)
            //    {
            //        OrderModel orderModel = new OrderModel();
            //        orderModel.SaleMoney = oneShop.orderSumprice;
            //        orderModel.shopName = storeBll.Search(x => x.storeid == oneShop.storeid).Select(x => new { x.storeid, x.storeName }).ToList().FirstOrDefault().storeName;
            //        dataList.Add(orderModel);//  
            //    }
            //}
            return Json(dataList,JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 柱状图 各个店铺销售额 
        /// </summary>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <returns></returns>
        public ActionResult GetOrderAll2(string date1, string date2)
        {
            List<OrderModel> dataList = new List<OrderModel>();
            Expression<Func<usersorder, bool>> condition = null;
            //  默认查询今天之前七天的记录 所有店铺的
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2))
            {
                DateTime todayNow = DateTime.Now.Date;
                DateTime midnight = DateTime.Now;
                midnight = midnight.Date.AddDays(+1);
                DateTime beforeWeek = DateTime.Now.Date.AddDays(-6);
                var query = bll.Search().ToList();
                var listOrder = query.Where(x => x.orderStatus == 1 && x.orderTime.Date <= todayNow && x.orderTime.Date >= beforeWeek).GroupBy(x => x.storeid).Select(x => new { discountMoney = x.Sum(y => y.discountMoney), orderSumprice = x.Sum(y => y.orderSumprice), x.FirstOrDefault().storeid, }).ToList();
                foreach (var oneShop in listOrder)
                {
                    OrderModel orderModel = new OrderModel();
                    orderModel.SaleMoney = oneShop.orderSumprice+oneShop.discountMoney;
                    orderModel.shopName = storeBll.Search(x => x.storeid == oneShop.storeid).Select(x => new { x.storeid, x.storeName }).ToList().FirstOrDefault().storeName;
                    dataList.Add(orderModel);
                }
            }
            //按日期查询  
            if (date1 != null && date1 != "" && date2 != null && date2 != "")
            {
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                condition = x => x.orderStatus == 1 && x.orderTime >= datetime1 && x.orderTime <= datetime2;
                var listOrder = bll.Search().Where(condition).GroupBy(x => x.storeid).Select(x => new { discountMoney = x.Sum(y => y.discountMoney), x.FirstOrDefault().orderCode, orderSumprice = x.Sum(y => y.orderSumprice), x.FirstOrDefault().storeid, }).ToList();
                foreach (var oneShop in listOrder)
                {
                    OrderModel orderModel = new OrderModel();
                    orderModel.SaleMoney = oneShop.orderSumprice+oneShop.discountMoney;
                    orderModel.shopName = storeBll.Search(x => x.storeid == oneShop.storeid).Select(x => new { x.storeid, x.storeName }).ToList().FirstOrDefault().storeName;
                    dataList.Add(orderModel);//  
                }
            }
            return Json(dataList, JsonRequestBehavior.AllowGet);
        }

        //优化sql  柱状图 各个店铺销售额 

        public ActionResult GetEveryShop(string date1, string date2)
        {
            List<OrderModel> dataList = new List<OrderModel>();
            //默认查询今天之前七天的记录 所有店铺的
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2))
            {
                DateTime todayNow = DateTime.Now;
                DateTime beforeWeek = DateTime.Now.Date.AddDays(-6);
                string sql = "select storeid,sum(discountMoney+orderSumprice) as SaleMoney  from usersorder FORCE index(idx_userorder_orderTime) where orderStatus=1 and orderTime BETWEEN '" + beforeWeek + "'  and '" + todayNow + "'  GROUP BY storeid";
                var list = bll.SearchBySQL<OrderModel>(sql).ToList();
                foreach (var oneShop in list)
                {
                    OrderModel orderModel = new OrderModel();
                    orderModel.SaleMoney = oneShop.SaleMoney;
                    orderModel.shopName = storeBll.Search(x => x.storeid == oneShop.storeid).Select(x => new { x.storeName }).FirstOrDefault().storeName;
                    dataList.Add(orderModel);
                }
            }
            //按日期查询  
            if (date1 != null && date1 != "" && date2 != null && date2 != "")
            {
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                string sql = "select storeid,sum(discountMoney+orderSumprice) as SaleMoney  from usersorder FORCE index(idx_userorder_orderTime) where orderStatus=1 and orderTime BETWEEN '" + datetime1 + "'  and '" + datetime2 + "'  GROUP BY storeid";
                var list = bll.SearchBySQL<OrderModel>(sql).ToList();
                foreach (var oneShop in list)
                {
                    OrderModel orderModel = new OrderModel();
                    orderModel.SaleMoney = oneShop.SaleMoney;
                    orderModel.shopName = storeBll.Search(x => x.storeid == oneShop.storeid).Select(x => new { x.storeName }).FirstOrDefault().storeName;
                    dataList.Add(orderModel);
                }
            }
            return Json(dataList, JsonRequestBehavior.AllowGet);
        }
    }
}
